package com.damacheng009.future.alarm.crawler;

import java.util.ArrayList;
import java.util.List;

import org.nutz.http.Header;
import org.nutz.json.Json;

import com.damacheng009.future.alarm.vo.CngoldKLineVo;
import com.damacheng009.future.alarm.vo.KLineVo;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;

public class CngoldFuture60minCrawler extends BaseCrawler {
	
	private static String URL_PATTERN = "https://api.jijinhao.com/sQuoteCenter/history.htm?code=%s&style=2&pageSize=100";
	
	@Override
	public List<KLineVo> getKLines(String code) {
		this.url = String.format(URL_PATTERN, code);
		Header header = Header.create();
		header.addv("referer", "http://m.cngold.org/home/dy110.html");
		String str = wget(header);
		List<CngoldKLineVo> cngoldKLines = Json.fromJsonAsList(CngoldKLineVo.class, str);
		
		List<KLineVo> klines = new ArrayList<>();
		
		cngoldKLines.forEach((cngoldVo) -> {
			KLineVo vo = new KLineVo();
			vo.setC(Convert.toStr(cngoldVo.getClose()));
			vo.setH(Convert.toStr(cngoldVo.getHigh()));
			vo.setL(Convert.toStr(cngoldVo.getLow()));
			vo.setO(Convert.toStr(cngoldVo.getOpen()));
			vo.setV(Convert.toStr(cngoldVo.getVolume()));
			
			long d = cngoldVo.getDate();
			
			vo.setD(DateUtil.date(d).toString());
			
			klines.add(vo);
		});
		
		return klines;
	}

	@Override
	protected String clean(String originString) {
		String tmp = originString.replace("var KLC_KL = {\"data\":", "");
		return tmp = tmp.substring(0, tmp.length() - 1).trim();
	}
	
}
